//用户登录
//var loginId; //dialog
//var loginformId; //formId
$(function () {
    /**
     * 登录校验
     */
    $("#checkId").click(function () {
        $("#loginFormId").validate({
            onsubmit: true,// 是否在提交是验证
            onfocusout: false,// 是否在获取焦点时验证
            onkeyup: false,// 是否在敲击键盘时验证
            rules: {
                username: "required",
                password: "required"
            },
            messages: {
                username: "*请输入用户名",
                password: "*请输入密码"
            },
            submitHandler: function (form) {//通过之后回调
                var username = $("#userName").val();
                var password = $("#password").val();
                $.ajax({
                    url: $WEB_ROOT_PATH + '/user/login',
                    type: "post",
                    dataType: "json",
                    data: {username: username, password: password},
                    success: function (data) {
                        console.log(data);
                        if (data.code === 200) {
                            $TokenUtil.setToken(data.tokenAz);
                            $TokenUtil.setSub(data.sub);
                            $TokenUtil.setPerm(data.perm);
                            // window.location.href = "/main/domain.html";
                            window.location.href = "/domain";
                            // $.ajax({
                            //     headers: {
                            //         'tokenAz': $TokenUtil.getToken()
                            //     },
                            //     url: $WEB_ROOT_PATH + '/domain',
                            //     type: 'get',
                            //     cache: false,
                            //     async: true,
                            //     success: function (result) {
                            //         console.log(result);
                            //     }
                            // });
                        } else {
                            $.messager.alert("提示", data.msg, "", function () {
                                //window.location.href = $WEB_ROOT_PATH + "/";
                            });
                        }
                    }
                });
            },
            invalidHandler: function (form, validator) {  //不通过回调
                return false;
            }
        });
    });

    //注销登录
    $("#logoutId").click(function () {
        $.messager.confirm("提示", "您确定要注销当前登录吗？", function (flag) {
            if (flag) {
                $TokenUtil.clearSub();
                $TokenUtil.clearToken();
                $TokenUtil.clearPerm();
                window.location.href = $WEB_ROOT_PATH + "/";
                // $.post($WEB_ROOT_PATH + '/w/layout', function (data) {
                //
                // });
            }
        });
    });
});